package com.yonyou.cloud.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import cn.hutool.json.JSONUtil;
import com.yonyou.cloud.model.LicenseEntity;
import com.yonyou.cloud.model.ResultEntity;
import com.yonyou.cloud.util.MachineUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * @author wangchu
 * @date 2024-04-25 15:03
 */
@RestController
public class LicenseController {

    @RequestMapping("/main.html")
    public ModelAndView main() {
        return new ModelAndView("main");
    }


    /**
     * 创建加密
     */
    @RequestMapping("/createLicense")
    public ResultEntity<?> createLicense(LicenseEntity entity, HttpServletRequest request) {
        // 数据校验
        // MAC地址
        if (StrUtil.isBlank(entity.getMac())) {
            return ResultEntity.fail("请输入机器MAC码！");
        }

        // 收费平台
        if (StrUtil.isBlank(entity.getPlatfprmType())) {
            return ResultEntity.fail("请选择收费平台加密方式！");
        }
        if (entity.getPlatfprmType().equals("1")) {
            if (Objects.isNull(entity.getUserNumber())) {
                return ResultEntity.fail("最大用户数量不能为空！");
            }
            if (entity.getUserNumber() <= 0) {
                return ResultEntity.fail("最大用户数量必须大于0！");
            }
        } else if (entity.getPlatfprmType().equals("2")) {
            if (StrUtil.isBlank(entity.getPlatformStartDate()) || StrUtil.isBlank(entity.getPlatformEndDate())) {
                return ResultEntity.fail("收费平台租赁开始时间、结束时间不能为空！");
            }
        } else {
            return ResultEntity.fail("收费平台加密方式无效！");
        }

        // 学生缴费移动端
        if (StrUtil.isBlank(entity.getStudentType())) {
            return ResultEntity.fail("请选择学生缴费移动端加密方式！");
        }
        if (entity.getStudentType().equals("1")) {
            if (Objects.isNull(entity.getStudentNumber())) {
                return ResultEntity.fail("最大学生数量不能为空！");
            }
            if (entity.getStudentNumber() <= 0) {
                return ResultEntity.fail("最大学生数量必须大于0！");
            }
        } else if (entity.getStudentType().equals("2")) {
            if (StrUtil.isBlank(entity.getStudentStartDate()) || StrUtil.isBlank(entity.getStudentEndDate())) {
                return ResultEntity.fail("学生缴费移动端租赁开始时间、结束时间不能为空！");
            }
        } else {
            return ResultEntity.fail("学生缴费移动端加密方式无效！");
        }

        // 外部人员缴费移动端
        if (StrUtil.isBlank(entity.getOuterType())) {
            return ResultEntity.fail("请选择外部人员缴费移动端加密方式！");
        }
        if (entity.getOuterType().equals("1")) {
            if (Objects.isNull(entity.getOuterNumber())) {
                return ResultEntity.fail("最大注册数量不能为空！");
            }
            if (entity.getOuterNumber() <= 0) {
                return ResultEntity.fail("最大注册数量必须大于0！");
            }
        } else if (entity.getOuterType().equals("2")) {
            if (StrUtil.isBlank(entity.getOuterStartDate()) || StrUtil.isBlank(entity.getOuterEndDate())) {
                return ResultEntity.fail("外部人员缴费移动端租赁开始时间、结束时间不能为空！");
            }
        } else {
            return ResultEntity.fail("外部人员缴费移动端加密方式无效！");
        }

        // 开始加密
        String baseInfo = JSONUtil.toJsonStr(entity);
        byte[] key = entity.getMac().getBytes(StandardCharsets.UTF_8);
        SymmetricCrypto des = new SymmetricCrypto(SymmetricAlgorithm.DESede, key);
        String encryptHex = des.encryptHex(baseInfo);
//        System.out.println(encryptHex);
        String decryptStr = des.decryptStr(encryptHex);
//        System.out.println(decryptStr);

        String uid = UUID.fastUUID().toString();
        request.getSession().getServletContext().setAttribute(uid, encryptHex);
        return ResultEntity.success("加密成功！", uid);
    }

    /**
     * 下载加密文件
     */
    @RequestMapping("/exportLicense")
    public void exportLicense(String uid, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String info = String.valueOf(request.getSession().getServletContext().getAttribute(uid));
        byte[] bytes = info.getBytes(StandardCharsets.UTF_8);

        response.setHeader("Content-Disposition","attachement; fileName=license.lic");
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            outputStream.write(bytes);
        }
    }

    /**
     * 获取本机机器码
     */
    @RequestMapping("/getLocalMac")
    public ResultEntity<?> getLocalMac() throws Exception {
        String macCodes = MachineUtil.getMachineCode();
        return ResultEntity.success("获取成功！", macCodes);
    }
}